package com.huanf.controller;

import com.alibaba.excel.EasyExcel;

import com.huanf.domain.WriterToExcelVo;
import com.huanf.service.WriterToExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.util.List;

/**
 * @author: 35238
 * 功能: easyExcel-把数据库的数据写入到Excel表格
 * 时间: 2023-12-27 14:14
 */
@RestController
@Slf4j
public class WriterToExcelController{

    @Autowired
    private WriterToExcelService writerToExcelService;

    @GetMapping("/writerToExcel") //浏览器访问 http://localhost:8082/writerToExcel
    public void image() throws Exception {

        //文件保存到哪里，还要指定文件名
        String PATH = "E:\\桌面\\Java-Ready\\conf\\Excel\\easyExcelWrite.xlsx";

        //从数据库查询数据
        List<WriterToExcelVo> list = writerToExcelService.list();

        //把数据写入到Excel中
        FileOutputStream fileOutputStream = new FileOutputStream((PATH));
        EasyExcel.write(fileOutputStream, WriterToExcelVo.class).autoCloseStream(Boolean.TRUE).sheet("文章分类").doWrite(list);
    }

}
